/*******************************************************************************
 *  * Copyright 2016 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package com.impetus.client.es.autogeneratedid;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import junit.framework.Assert;

public class IdAutoGenerationTest
{

    private EntityManagerFactory emf;

    private EntityManager em;

    private static Node node = null;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        Builder builder = Settings.settingsBuilder();
        builder.put("path.home", "target/data");
        node = new NodeBuilder().settings(builder).node();
    }

    @Before
    public void setup()
    {

        emf = Persistence.createEntityManagerFactory("es-auto-gen-pu");
        em = emf.createEntityManager();
    }

    @Test
    public void test() throws InterruptedException
    {

        Employee emp1 = new Employee();
        emp1.setName("dev");
        emp1.setSalary(50000);

        Employee emp2 = new Employee();
        emp2.setName("karthik");
        emp2.setSalary(60000);

        em.persist(emp1);
        em.persist(emp2);

        waitThread();

        List employees = em.createQuery("select e from Employee e").getResultList();
        Assert.assertEquals(2, employees.size());
        assertEmployees(employees);

        Query q = em.createQuery("delete from Employee e");
        int count = q.executeUpdate();
        Assert.assertEquals(2, count);

    }

    private void assertEmployees(List<Employee> employees)
    {

        for (Employee e : employees)
        {
            if (e.getName().equals("dev"))
            {
                Assert.assertEquals(new Integer(50000), e.getSalary());
            }
            else if (e.getName().equals("karthik"))
            {
                Assert.assertEquals(new Integer(60000), e.getSalary());
            }
        }

    }

    @After
    public void tearDown()
    {
        if (em != null)
        {
            em.close();
        }

        if (emf != null)
        {
            emf.close();
        }

    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
        node.close();
    }

    /**
     * Wait thread.
     */
    private void waitThread()
    {
        try
        {
            Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

}
